package com.imooc.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



/**
 * 利用请求头开发多端应用
 * 重要方法：request.getHeader("User-Agent");获取请求头部分的UserAgent字符串信息
 * 用userAgent.indexOf("Windows NT") != -1
 * 根据设备信息字符串里面是windows还是iPhone或Android，输出不同的页面代码
 * @author zhao
 *
 */

@WebServlet("/ua")
public class UserAgentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
 
    public UserAgentServlet() {
        super();
    }


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取请求头部分的UserAgent，用户代理字符串，它里面有访问设备的相关信息
		String userAgent = request.getHeader("User-Agent");
		response.setContentType("text/html;charset = utf-8");
		//输出请求头部分，UserAgent字符串里面的设备相关信息
		response.getWriter().println(userAgent);
		String output = "";
		if(userAgent.indexOf("Windows NT") != -1) {
			output = "<h1>这是PC端首页</h1>";
		}else if(userAgent.indexOf("iPhone") != -1 || userAgent.indexOf("Android") != -1){
			output = "<h1>这是移动端首页</h1>";
		}
		response.getWriter().println(output);
	}

}
